home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 270 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.8 KB

  1. Path: fido.asd.sgi.com!austern
  2. From: John Max Skaller <maxtal@suphys.physics.su.oz.au>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Cleaning auto_ptr copy semantics.
  5. Date: 05 Feb 1996 09:29:02 PST
  6. Organization: MAXTAL
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <3115374C.1BC7@suphys.physics.su.oz.au>
  9. References: <gregorDLrrun.K2x@netcom.com> <4etq2h$acg@hermes.synopsys.com>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Mon, 05 Feb 1996 08:46:36 +1000
  12. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMRY+bUy4NqrwXLNJAQHT3wIAijFpOVG/ohHLTeV1AXhvPmv4kfXdHDvl
  15.     2kx5y5xtX+zaSgO6y4HvrqDKc85FhT1CyF7va1sxI/PBLw8wC2289g==
  16.     =74To
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. Joe Buck wrote:
  20. > gregor@netcom.com (Greg Colvin) writes:
  21. > >The smallest change I can see that preserves the semantics of strict
  22. > >ownership is to separate the concepts of "holding a pointer" and "owning
  23. > >an object", so that more than one auto_ptr can hold a pointer to an object,
  24. > >but only one auto_ptr can own the object.
  25. > Your new proposal is a recipe for dangling pointers.  
  26.  
  27.     It is not Greg's current proposal which is
  28. a recipe for disaster, it is the very notion of using
  29. the copy operator to transfer ownership. IMHO.
  30.  
  31.  
  32. > I don't see a way around reference counts if you want a class that
  33. > contains a pointer and can do copies and assignments using the expected
  34. > semantics and guarantees to clean up the heap object.
  35.  
  36.     I agree completely.
  37.  
  38. > We could still have plain auto_ptr, but with no assignment operator and no
  39. > copy constructor, and with an explicit member function for transferring
  40. > ownership.  This means that a function cannot return an auto_ptr, but it
  41. > could still return one through a reference parameter.
  42.  
  43.     Again, I agree completely, this was my original proposal:
  44. TWO classes, one which was to be used simply to attach heap objects onto
  45. the stack to allow the destructor to delegate to destroying the
  46. heap object. This fixes the problem:
  47.  
  48.     void f() {
  49.         X* x= new X;
  50.         throw 1; // WOOPS, 
  51.         delete x;
  52.     }
  53.  
  54.     Since C++ stack frames are not copyable, copyability
  55. in auto_ptr is not only not required, it's dangerous. The concept
  56. is simply to SCHEDULE deletion at the point of construction:
  57.  
  58.     void f() {
  59.         auto_ptr<X> x (new X); // SCHEDULES DELETION at #1
  60.         throw 1; // OK: invokes block termination code
  61.         // #1: termination code "delete x;" scheduled
  62.     }
  63.  
  64.     This leaves the problem of pointers in copyable
  65. objects -- for which reference counting is a solution
  66. in acyclic cases.
  67.  
  68.     The problem with the "move" semantics of
  69. the current auto_ptr is NOT the strict ownership
  70. idiom -- it's the use of inappropriate copy syntax to
  71. access that idiom.
  72.  
  73.     The solution -- IMHO -- is to ignore and NOT USE
  74. auto_ptr, and take up where the committee failed:
  75. attempt to obtain consensus amoung USERS on suitable
  76. smart pointer classes. "Practice" will then push such
  77. accepted and "in use" classes in to the _next_ standard.
  78.  
  79.     My UESTL/smart module is an attempt to provide
  80. a coherent set of smart pointers. Mail me for a copy,
  81. email comments welcome. [It's unclear if the charter of this
  82. newsgroup allows for "public" comment on my "private"
  83. attempt at standardisation. But the results will be published
  84. in black and white by Prentice Hall. Someone has to
  85. do this, since the committee wasn't able to. The set of
  86. classes I have shows why the committe had trouble: more
  87. than one class is required.]
  88.  
  89. -- 
  90. John Max Skaller               voice: 61-2-566-2189
  91. 81 Glebe Point Rd              fax:   61-2-660-0850
  92. GLEBE NSW 2037                 web: http://www.maxtal.com.au/~skaller/
  93. AUSTRALIA                      email: skaller@maxtal.com.au
  94. ---
  95. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  96.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  97.   in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  98.